<?php 
include ("config.inc.php");
function disp_full_name_admin(){
	if(!isset($_SESSION['full_name_admin']) || trim($_SESSION['full_name_admin']) == ''){
	    $sql    = "SELECT fname,lname,a_id FROM admin WHERE a_id='".$_SESSION['admin_id']."' LIMIT 1";
		$result = mysql_query($sql) or die(mysql_error());
		$data   = mysql_fetch_assoc($result);
		$_SESSION['full_name_admin'] = $data['fname'] . ' ' . $data['lname'];
	}
	return $_SESSION['full_name_admin'];

}
function client_count(){
	if(!isset($_SESSION['client_count']) || trim($_SESSION['client_count']) == ''){
	    $sql    = "SELECT c_id FROM clients";
		$result = mysql_query($sql) or die(mysql_error());
		$count  = mysql_num_rows($result);
	}
	return $count;

}
function project_count(){
	if(!isset($_SESSION['project_count']) || trim($_SESSION['project_count']) == ''){
	    $sql    = "SELECT p_id FROM projects WHERE date_finished IS NULL";
		$result = mysql_query($sql) or die(mysql_error());
		$count  = mysql_num_rows($result);
	}
	return $count; 

}
function client_update_count(){
	if(!isset($_SESSION['client_update_count']) || trim($_SESSION['client_update_count']) == ''){
	    $sql    = "SELECT u_id FROM updates WHERE date_completed IS NULL";
		$result = mysql_query($sql) or die(mysql_error());
		$count  = mysql_num_rows($result);
	}
	return $count;

}
function client_update_count_claimed(){
	if(!isset($_SESSION['client_update_count_claimed']) || trim($_SESSION['client_update_count_claimed']) == ''){
	    $sql    = "SELECT u_id FROM updates WHERE a_id='".$_SESSION['admin_id']."'";
		$result = mysql_query($sql) or die(mysql_error());
		$count  = mysql_num_rows($result);
	}
	return $count;

}
function last_login(){ 
	if(!isset($_SESSION['last_login']) || trim($_SESSION['last_login']) == ''){
	    $sql    = "SELECT last_login FROM admin WHERE a_id='".$_SESSION['admin_id']."'";
		$result = mysql_query($sql) or die(mysql_error());
		$data   = mysql_fetch_assoc($result);
		$date   = date('l - F j g/i/s', strtotime($row['date_started']));
		$_SESSION['last_login'] = $date;
	}
	return $_SESSION['last_login'];

}
function disp_latest_projects(){
	$sql    = "SELECT projects.p_title,projects.p_id,projects.date_started,clients.fname,clients.lname
	 		   FROM projects 
			   INNER JOIN clients ON projects.c_id = clients.c_id
			   ORDER BY date_started DESC
			   LIMIT 5";
	$result = mysql_query($sql) or die('Error is: '. mysql_error());
	$count  = mysql_num_rows($result);
	while($data = mysql_fetch_array($result)){
		echo '<div class="project_'.$data['p_id'].'">';
		echo '<h4>'.$data['p_title'].' - '.$data['fname'].' '.$data['lname'].'</h4><span class="quiet" id="n">'.timeDifferenceLatestProjects($data['p_id']).'</span><br />';
		echo '</div>';
		echo '<hr />';
	}                                              
	return;
}
function disp_all_notes(){
	if (isset($_SESSION['last_login'])){
           $sql = "SELECT n.c_id, n.date, n.status, n.note, a.last_login, c.fname,c.lname
                           FROM notes n
                           INNER JOIN clients c ON n.c_id = c.c_id
                           INNER JOIN admin a ON a.a_id = '" .$_SESSION['admin_id']."'
                           WHERE n.date >= '" . $_SESSION['last_login'] . "'
                           AND n.status = '1'";
        }else{
           $sql = "SELECT n.c_id, n.date, n.status, n.note, a.last_login, c.fname,c.lname
                           FROM notes n
                           INNER JOIN clients c ON n.c_id = c.c_id
                           INNER JOIN admin a ON a.a_id = '" .$_SESSION['admin_id']."'
                           WHERE n.status = '1'";
       } 
    $result = mysql_query($sql) or die('Error is: '. mysql_error());
	$count  = mysql_num_rows($result);
	if($count == 0){
		print "No notifications from clients since last login. View past <a href='?c=notes' title='View all notifications'>notifications</a>";
	}else{       
		while($data = mysql_fetch_array($result)){
 
				//$unix_timestamp = strtotime($data["date"]);
				//$data['date'] = date('l - n/j H:i a', $unix_timestamp);
				
				echo '<div class="note_'.$data['c_id'].'">';
				echo '<h4>'.$data['fname'].' '.$data['lname'].'</h4><span class="quiet" id="n">'.$data['note'].'</span><br />';
				echo '&nbsp;&nbsp;&nbsp;<span class="small">'.timeDifferenceNotes($data['c_id']).'</span>';
				echo '</div>';
				echo '<br /><hr />';
		} 
	}                                              
	return;
}
function work_time(){
	  // old query  $sql    = "SELECT SUM(minutes) FROM work WHERE a_id='".$_SESSION['admin_id']."'";
	    $sql    = "SELECT concat(floor(minutes/60),':',lpad(minutes%60,2,'0')) as hours FROM work WHERE workdate > date_format(curdate(),'%Y-%m-01') AND a_id='".$_SESSION['admin_id']."'";		$result = mysql_query($sql) or die(mysql_error());
		$data   = mysql_fetch_assoc($result);
		$total_time = $data['hours'];
	
	
	return $total_time;
}
?>